package com.example.module_motorcircle.publish

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.Router
import com.example.lib_common.base.BaseToolBarActivity
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.state.UIState
import com.example.lib_common.utils.ToastUtils
import com.example.module_motorcircle.R
import com.example.module_motorcircle.databinding.ActivityPublishPostBinding
import com.example.module_motorcircle.publish.viewmodel.PostPublishViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.io.File

@Route(path = Router.MOTOR_CIRCLE_PUBLISH_POST_ACTIVITY)
@AndroidEntryPoint
class PublishPostActivity : BaseToolBarActivity<PostPublishViewModel,ActivityPublishPostBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_publish_post
    override val mViewModel: PostPublishViewModel by viewModels()

    override fun initData() {
        mBinding.viewModel = mViewModel

    }

    override fun initListener() {
        mBinding.etPost.addTextChangedListener {
            mBinding.toolbar.tvPublish.isEnabled = !it.isNullOrEmpty()
        }
        //订阅发布帖子结果
        mViewModel.publishPostState_.observeWithLifecycle(this){
            when(it){
                is UIState.Loading -> {
                    setLoadingViewVisible(true)
                }
                is UIState.Success -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"发布成功")
                    //发送事件
                    //关闭页面
                }
                is UIState.Error -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"发布失败 $it")
                    Log.d("TAG","发布失败 $it")
                }
                else -> {}
            }
        }
        //发布帖子
        mBinding.toolbar.tvPublish.setOnClickListener {
            //无图片
//            mViewModel.publishPost()
            //发布图片
            val files = mutableListOf<File>()
            files.add(File("/sdcard/Pictures/Screenshot_20240924_172221.png"))
            files.add(File("/sdcard/Pictures/Screenshot_20240924_172457.png"))
            files.add(File("/sdcard/Pictures/Screenshot_20240924_172542.png"))
            files.add(File("/sdcard/Pictures/Screenshot_20240924_172845.png"))
            mViewModel.publishPostWithFiles(files,false)
            //发布视频
        }
    }

}